/*
 * User.cpp
 *
 */

#include "User.h"

User::User() {
	dni = 0;
	firstName = "";
	lastName = "";
	password = "";
	province = "";
	type = '0';
}

User::~User() {
}

User::User(BigInt dni, const String& firstName, const String& lastName, const String& password, const String& province, char type) {
	this->dni = dni;
	this->firstName = firstName;
	this->lastName = lastName;
	this->password = password;
	this->province = province;
	this->type = type;
}

User::User(const User& copy) {
	dni = copy.dni;
	firstName = copy.firstName;
	lastName = copy.lastName;
	password = copy.password;
	province = copy.province;
	type = copy.type;
}

User& User::operator =(const User& other) {
	dni = other.dni;
	firstName = other.firstName;
	lastName = other.lastName;
	password = other.password;
	province = other.province;
	return *this;
}

bool User::isValid() const {
	return dni > 0 && firstName.length() <= FIRSTNAME_LENGTH && lastName.length() <= LASTNAME_LENGTH &&
			province.length() <= PROVINCE_LENGTH && password.length() <= PASSWORD_LENGTH && isTypeValid(type);
}

bool User::isTypeValid(char type) const {
	return type == 'A' || type == 'P' || type == 'U';
}

void User::serialize(ISerializer& serializer) const {
	serializer.serializeUnsignedLongInt(dni);
	serializer.serializeString(firstName);
	serializer.serializeString(lastName);
	serializer.serializeString(password);
	serializer.serializeString(province);
	serializer.serializeUnsignedLongInt(emails.size());
	for (StringVector::const_iterator it = emails.begin(); it != emails.end(); ++it) {
		serializer.serializeString(*it);
	}
	serializer.serializeChar(type);
}

void User::unserialize(IUnserializer& unserializer) {
	dni = unserializer.unserializeUnsignedLongInt();
	firstName = unserializer.unserializeString();
	lastName = unserializer.unserializeString();
	password = unserializer.unserializeString();
	province = unserializer.unserializeString();
	BigInt count = unserializer.unserializeUnsignedLongInt();
	for (BigInt x = 0; x < count; x++) {
		emails.push_back(unserializer.unserializeString());
	}
	type = unserializer.unserializeChar();
}

BigInt User::size() const {
	BigInt size = 2*sizeof(BigInt) + 4 + firstName.length() + password.length() + lastName.length() + province.length() + 1;
	for (StringVector::const_iterator it = emails.begin(); it != emails.end(); ++it) {
		size += it->length();
	}
	return size;
}

BigInt User::getHashcode() const {
	return dni;
}
